package lamnhungoc.form.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lamnhungoc.pojo.KhachHang;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

@Component("dangKyFormValidator")
public class DangKyFormValidator implements Validator{

	@Override
	public boolean supports(Class clazz) {
		// TODO Auto-generated method stub
		return KhachHang.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object object, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tenDangNhap","required.tenDangNhap");
		KhachHang kh = (KhachHang) object;
        if (kh.getDiaChi().length() > 20)
        {
                errors.rejectValue("diaChi", "required.diaChi");
        }
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher m = p.matcher(kh.getEmail());
        boolean matchFound = m.matches();

        if (!matchFound)
        {
			errors.rejectValue("email", "error_invalid_email");
        }
		
	}

}
